package ops.weixin.cp.api.impl;

import static ops.weixin.cp.constant.WxCpApiPathConsts.Department.DEPARTMENT_LIST;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import ops.weixin.common.error.WxErrorException;
import ops.weixin.cp.api.WxCpDepartmentService;
import ops.weixin.cp.api.WxCpService;
import ops.weixin.cp.bean.WxCpDepart;

/**
 * 部门相关接口
 * 
 * @author SuM
 * @date: 2019年12月25日下午1:23:20
 */
public class WxCpDepartmentServiceImpl implements WxCpDepartmentService {
	private final WxCpService mainService;

	public WxCpDepartmentServiceImpl(WxCpService mainService) {
		this.mainService = mainService;
	}

	@Override
	public List<WxCpDepart> list(Long id) throws WxErrorException {
		String url = this.mainService.getWxCpConfigStorage().getApiUrl(DEPARTMENT_LIST);
		if (id != null) {
			url += "?id=" + id;
		}

		String responseContent = this.mainService.get(url, null);
		JSONObject jsonObject = JSONObject.parseObject(responseContent);
		JSONArray jsonArray = (JSONArray) jsonObject.get("department");
		return (List<WxCpDepart>) JSONArray.parseArray(jsonArray.toString(), WxCpDepart.class);
	}
}
